package GUI.Productos;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.TableColumn;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import article.entities.ArticleVO;
import article.entities.ComboVO;
import article.entities.IngredientVO;
import article.exceptions.BadCategoryException;
import article.exceptions.MissingCategoryException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.article.interfaces.ArticleProxyMgt;

import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import dbExceptions.MissingComboException;

public class NuevoCombo extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	private Table table_1;
	private Text text;
	private Text text_1;
	private Label error;
	private Text text_2;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public NuevoCombo(Shell parent, int style) {
		super(parent, style);
		setText("SWT Dialog");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(669, 496);
		shell.setText(getText());
		shell.setLayout(null);
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(10, 137, 194, 267);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(191);
		tblclmnNewColumn.setText("Articulos a Agregar");
		
		table_1 = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table_1.setLinesVisible(true);
		table_1.setHeaderVisible(true);
		table_1.setBounds(469, 137, 194, 267);
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table_1, SWT.NONE);
		tblclmnNewColumn_1.setWidth(193);
		tblclmnNewColumn_1.setText("Articulos en el Nuevo Combo");
		
		Button btnAgregarAlCombo = new Button(shell, SWT.NONE);
		btnAgregarAlCombo.addSelectionListener(new SelectionAdapter() {
		
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table.getSelection()[0];
				TableItem t1 = new TableItem(table_1, SWT.NONE);
				t1.setText(0, t.getText());
				//table.remove(table.getSelectionIndex());
			}
		});
		btnAgregarAlCombo.setBounds(273, 220, 122, 28);
		btnAgregarAlCombo.setText("Agregar al combo");
		
		Button btnQuitarDelCombo = new Button(shell, SWT.NONE);
		btnQuitarDelCombo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem t = table_1.getSelection()[0];
				table_1.remove(table_1.getSelectionIndex());
			//	TableItem tt = new TableItem(table, SWT.NONE);
				//tt.setText(t.getText());
			}
		});
		btnQuitarDelCombo.setBounds(273, 267, 122, 28);
		btnQuitarDelCombo.setText("Quitar del Combo");
		
		Button btnCancelar = new Button(shell, SWT.NONE);
		btnCancelar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.dispose();
			}
		});
		btnCancelar.setBounds(406, 436, 94, 28);
		btnCancelar.setText("Cancelar");
		
		Button btnAceptarYGuardar = new Button(shell, SWT.NONE);
		btnAceptarYGuardar.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem[] combo = table_1.getItems();
				ArticleVO [] arts= null;
				ArticleProxyMgt o = ServiceFacade.getInstance().getArticleProxyMgt();
				try {
					arts= o.getArticles();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				ArrayList<ArticleVO> lista = new ArrayList<ArticleVO>();
				for(int i=0;i<combo.length;i++){
					for (int j=0;j<arts.length;j++){
						if(combo[i].getText().equalsIgnoreCase(arts[j].getName())){
							lista.add(arts[j]);
							
						}
					}
					
				}
				boolean ok =true;
				String n = text.getText();
				if (n.equalsIgnoreCase("")){
					error.setText("Ingrese un Nombre para el Combo");
					ok=false;
				}
				String p = text_1.getText();
				double precio = 0;
				if(p.equalsIgnoreCase("")){
					error.setText("Ingrese un precio para el Combo");
					ok=false;
				}
				else{
					precio = Double.parseDouble(p);
				}
				if(ok==true){
					String d = text_2.getText();
					ComboVO comb = new ComboVO(n, d, precio,  lista);
				try {
					o.newCombo(comb);
					shell.dispose();
				} catch (RemoteException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (MissingCategoryException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (MissingComboException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (BadCategoryException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				} catch (ProxyException e1) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e1.printStackTrace();
				}
				}
			
			}
		});
		btnAceptarYGuardar.setBounds(127, 436, 122, 28);
		btnAceptarYGuardar.setText("Aceptar y Guardar");
		
		Label lblNombreDelNuevo = new Label(shell, SWT.NONE);
		lblNombreDelNuevo.setBounds(124, 20, 145, 14);
		lblNombreDelNuevo.setText("Nombre del Nuevo Combo");
		
		Label lblPrecioDelCombo = new Label(shell, SWT.NONE);
		lblPrecioDelCombo.setBounds(124, 55, 103, 14);
		lblPrecioDelCombo.setText("Precio del Combo");
		
		text = new Text(shell, SWT.BORDER);
		text.setBounds(288, 17, 145, 19);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setBounds(288, 52, 145, 19);
		
		Label label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label.setBounds(10, 410, 652, 26);
		
		error = new Label(shell, SWT.NONE);
		error.setBounds(220, 335, 227, 14);
		error.setText("");
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setBounds(127, 85, 142, 14);
		lblNewLabel.setText("Descripcion del Combo");
		
		text_2 = new Text(shell, SWT.BORDER);
		text_2.setBounds(288, 85, 145, 19);
		
		Label label_1 = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_1.setBounds(10, 110, 653, 28);
		ArticleProxyMgt uso = ServiceFacade.getInstance().getArticleProxyMgt();
		ArticleVO[] ings=null;
		try {
			ings = uso.getArticles();
		} catch (RemoteException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		} catch (ProxyException e) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e.printStackTrace();
		}
		for(int i=0;i<ings.length;i++){
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(ings[i].getName());
		}
		
	}
}
